#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <cstring>
#include <vector>
#include<queue>
using namespace std;
#define MAXL 1000005
#define MAXLL 0x3f3f3f
int a[MAXL];
int main()
{
	int n,s,x,y;
	cin >> n;
	while (n--)
	{
		cin >> s;
		if (s == 1)
		{
			cin >> x >> y;
			int pp = a[x];
			a[x] = y;
			a[y] = pp;
		}
		else if (s == 2)
		{
			cin >> x;
			cout << a[x] << endl;
		}
		else {
			cin >> x;   
			int p;
			p = a[a[x]];
			a[a[x]] = 0;
			a[x] = p;
		}
	}
	return 0;
}